package cn.spike.basic.controller;

import cn.spike.basic.dto.PhoneCodeDto;
import cn.spike.basic.exception.BusinessException;
import cn.spike.basic.service.IVerifyCodeService;
import cn.spike.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 公共接口：验证码接口 - 生成验证码
 * @author:spike
 * @create:2023-02-07 16:45
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 生成图形验证码的接口
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key")String key){
        try {
            //业务获取到图形验证码的base64编码的字符串
            String base64Str = verifyCodeService.getImageCode(key);
            //通过ajaxresult的obj参数传输到前端 - 方便前端显示图形验证码
            return AjaxResult.me().setObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @PostMapping("/phone")
    public AjaxResult getPhoneCode(@RequestBody PhoneCodeDto phoneCodeDto){
        try {
            verifyCodeService.getPhoneCode(phoneCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


    @PostMapping("/binderSmsCode")
    public AjaxResult getBinderSmsCode(@RequestBody PhoneCodeDto dto){
        try {
            verifyCodeService.getBinderSmsCode(dto);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }



}
